package com.SinaMicroblogging.Extension;

import java.io.IOException;
import java.util.List;

import com.SinaMicroblogging.R;
import com.SinaMicrobloggingApi.Model.DirectMessagesModel;
import com.SinaMicrobloggingApi.Model.StatusModel;
import com.SinaMicrobloggingApi.Model.UserInfoModel;
import com.Util.HttpUtility;
import com.Util.String2;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils.TruncateAt;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class DirectMessagesModelAdapter extends BaseAdapter {

	private List<DirectMessagesModel> directMessages;
	private LayoutInflater layoutInflater;
	private UserInfoModel user;
	private DirectMessagesModel message;
	private OnClickListener replyClick;

	public OnClickListener getReplyClick() {
		return replyClick;
	}

	public void setReplyClick(OnClickListener replyClick) {
		this.replyClick = replyClick;
	}

	public DirectMessagesModelAdapter(List<DirectMessagesModel> directMessages,
			Context context) {
		this.directMessages = directMessages;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return directMessages == null ? 0 : directMessages.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return directMessages.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return directMessages.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.messagesitemview,
					null);
			viewHolder = new ViewHolder();
			viewHolder.avatar = (ImageView) convertView
					.findViewById(R.id.avatar);
			viewHolder.title = (TextView) convertView.findViewById(R.id.title);
			viewHolder.info = (TextView) convertView.findViewById(R.id.info);
			viewHolder.detail = (TextView) convertView
					.findViewById(R.id.detail);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		message = directMessages.get(position);
		boolean expandable = message.getExpandable();
		if (expandable) {
			viewHolder.info.setSingleLine(false);
			viewHolder.info.setEllipsize(null);
			viewHolder.info.setHorizontallyScrolling(false);
			ViewGroup group = (ViewGroup) convertView;
			TextView replyMsg = new TextView(convertView.getContext());
			LayoutParams layoutParams = new RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.FILL_PARENT);
			layoutParams.addRule(RelativeLayout.BELOW, R.id.detail);
			layoutParams.addRule(RelativeLayout.ALIGN_RIGHT);
			replyMsg
					.setText(Html
							.fromHtml("<a style=\"color:blue;\"href=\"reply\">reply</a>"));
			replyMsg.setAutoLinkMask(Linkify.WEB_URLS);
			replyMsg.setMovementMethod(LinkMovementMethod.getInstance());
			replyMsg.setAutoLinkMask(Linkify.WEB_URLS);
			replyMsg.setClickable(true);
			replyMsg.setOnClickListener(replyClick);
			replyMsg.setGravity(Gravity.RIGHT);
			group.addView(replyMsg, layoutParams);
		} else {
			viewHolder.info.setSingleLine(true);
			viewHolder.info.setEllipsize(TruncateAt.END);
			viewHolder.info.setHorizontallyScrolling(true);
			ViewGroup group = (ViewGroup) convertView;
			int childCount = group.getChildCount();
			if (childCount == 7) {
				group.removeViewAt(childCount - 1);
			}
		}
		user = message.getSender();
		if (user.getAvatarImg() == null) {
			Log.i("sinaweibo", String.format("%s_set defaultavatar", position));
			viewHolder.avatar.setImageResource(R.drawable.defaultavatar);
		} else {
			Log.i("sinaweibo", String.format("%s_set img", position));
			viewHolder.avatar.setImageBitmap(user.getAvatarImg());
		}
		viewHolder.title.setText(user.getName());
		viewHolder.info.setText(message.getText());
		viewHolder.detail.setText(message.getCreateAtTimestamp());
		return convertView;
	}

	static class ViewHolder {
		ImageView avatar;
		TextView title;
		TextView info;
		TextView detail;
	}
}
